<?php

namespace App\Http\Controllers\Merchants;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class ShoplistController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
         //显示商品列表
        $data = DB::table("goods")->where('mid',$id)->paginate(5);
        $lan = DB::table("foodbar")->get();
        return view('merchant.shoplist')->with('data',$data)->with('lan',$lan);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //

    }


        //删除商品

     public function delete($id)
    {
        //
          

               DB::table('goods')->where('id',$id)->delete();
            return back();
        
    }


    //编辑商品



     public function updata($id)
    {
        //
          

                
                $cats = DB::table('cats')->get();

                 $data = DB::table("goods")->where('id',$id)->get();
                $col = DB::table('foodbar')->get();
                return view('merchant.shopupdata')->with('data',$data)->with('cats',$cats)->with('col',$col);
            
        }
        //商品信息修改
       public function doupdata(Request $request)
        {
        
          

            $file = $request->file('photo');
        if($file){
            $id=$request->id;
             //确认上传的文件是否成功
            if($file->isValid()){

                //$picname = $file->getClientOriginalName(); //获取上传原文件名
                $ext = $file->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $photo_path = "images/photo/";
                $filename = time().rand(1000,9999).".".$ext;
                $file->move($photo_path,$filename);
                $url = $photo_path.$filename;

          

            

                $id=$request->id;
                $arr = $request->all();
                $arr = array_except($arr, ['_token']);
                $arr = array_except($arr, ['photo']);
                $arr = array_except($arr, ['id']);
                $arr = array_add($arr, 'photo',$url);

       
              $new= DB::table('goods')
              ->where('id',$id)
                ->update($arr);
               return back();

              }
         

            }else{

                $id=$request->id;
                $arr = $request->all();
                $arr = array_except($arr, ['_token']);
                $arr = array_except($arr, ['photo']);
                $arr = array_except($arr, ['id']);
             

       
              $new= DB::table('goods')
              ->where('id',$id)
                ->update($arr);
                return back();
            }
    
    }


    //根据商品类别展示
    public function lei(Request $request){
        $src = $request->name;
        $mid = session('homeInfo')->id;
        
        if($src){
            $data = DB::table('goods')->where('column_id',$src)->where('mid',$mid)->paginate(2);
          
          
        
        foreach($data as $key=>$da){
           
                $arr[] = $da;
           
        }
       
        
           return (['1'=>$arr,'2'=>$data->render()]);
        }
    
    }


    public function fenLei($id){
        $src = $id;
         $mid = session('homeInfo')->id;
        $data = DB::table('goods')->where('column_id',$src)->where('mid',$mid)->paginate(2);
          $lan = DB::table("foodbar")->get();

        // foreach($data as $key=>$da){
           
        //         $arr[] = $da;          
        // }
        //     return (['1'=>$arr,'2'=>$data->render()]);
        return view('merchant.shoplist')->with('data',$data)->with('lan',$lan);
    }
}




